import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This class represents the power bar which gives us the value of loads. Positive (red line) gives a charge of 1
 * and negative (blue line) gives a load of 0.
 * 
 * @author Christian Pérez Zavalija 
 * @version 25-Jun-2013
 */
public class PowerBar extends Actor
{
    private GreenfootImage imgPowerBar;
    private int charge;
    
    /**
     * Load the image and displays.
     */
    public PowerBar()
    {
        imgPowerBar=new GreenfootImage("powerbar.png");
        setImage(imgPowerBar);
    }
    
    /**
     * makes the charge is equal to 0.
     * 
     * @return the charge = 0 if left clicked on the power bar.
     */
    public int Negative()
    {
        return charge=0;
    }
    
    /**
     * makes the charge is equal to 1.
     * 
     * @return the charge = 1 if right clicked on the power bar.
     */
    public int Positive()
    {
        return charge=1;
    }
    
    /**
     * You can drag the power bar and when you click on it depending on which side of the bar do the click,
     * will charge the destination to which you connect and then calls the method pinSelecc ().
     */
    public void act() 
    {
        MouseInfo mouse=Greenfoot.getMouseInfo();
        if(Greenfoot.mouseDragged(this))    //Condition so that the object can be dragged with the mouse while pressing the left button.
        {
            setLocation(mouse.getX(),mouse.getY()-150);
        }
        if(!Greenfoot.mouseDragged(this))
            if(Greenfoot.mouseClicked(this)){
                World world=getWorld();
                Canvas canvas=(Canvas)world;
                Pin currentPin=new Pin(1);
                getWorld().addObject(currentPin,mouse.getX(),mouse.getY());
                if(mouse.getX()<=this.getX())       //condition to know which side is doing the click
                    currentPin.charge=1;
                else
                    currentPin.charge=0;
                canvas.pinSelecc(currentPin);
            }
    }
}
